<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="BCR编号" prop="spearId">
        <el-input
          v-model="queryParams.spearId"
          placeholder="请输入手持枪编号"
          clearable
          size="large"
          :disabled="edit"
        />
      </el-form-item>
      <el-form-item label="条码编号" prop="contNo">
        <el-input
          v-model="queryParams.contNo"
          placeholder="请输入条码编号"
          clearable
          size="large"
          @keyup.enter.native="handleQuery"
          style="width:400px;"
          ref="clikPosition"
        />
      </el-form-item>
     

      <el-form-item>
        <!-- <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button> -->
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">Replace</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="WcsPceditList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
     <!-- <el-table-column label="ID" align="center" prop="trkId"  />  -->
      <el-table-column label="BCR编号" align="center" prop="spearId" width="120" />
      <el-table-column label="条码编号" align="center" prop="contNo" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="计划目的地" align="center" prop="toPos" width="160" />
      <!-- <el-table-column label="实际目的地" align="center" prop="curPos"  /> -->
      <!-- <el-table-column label="标识" align="center" prop="dealFlag"  /> -->
    <el-table-column label="录入时间" align="center" prop="createDate" min-width="160" :show-overflow-tooltip="true">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
    </el-table-column>
      
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:WcsPcedit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:WcsPcedit:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

 
  </div>
</template>

<script>
import { listWcsPcedit, getWcsPcedit, delWcsPcedit, addWcsPcedit, updateWcsPcedit, exportWcsPcedit } from "@/api/system/WcsPcedit";

export default {
  name: "WcsPcedit",
  data() {
    return {
       timer: '',//定时器
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      readonly: true,
      edit: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据录入表格数据
      WcsPceditList: [],
        // 日期范围
        dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // ID字典
      trkIdOptions: [],
      // 条码编号字典
      contNoOptions: [],
      // 计划目的地字典
      toPosOptions: [],
      // 实际目的地字典
      curPosOptions: [],
      // 标识字典
      dealFlagOptions: [],
      // 录入时间字典
      createDateOptions: [],
      // 更新时间字典
      updateDateOptions: [],
      // 手持强编号字典
      spearIdOptions: [],
      // 查询参数
      queryParams: {
        contNo: null,
        spearId: 30,
        // pageNum: 1,
        // pageSize: 10,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  mounted() {
    this.getList();
    this.timer = setInterval(this.getVisualData
    , 1000);

    //自动获取焦点
    this.$nextTick(() =>{
      this.$refs.clikPosition.focus()
    })
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  
  methods: {
    /** 查询数据录入列表 */
    getList() {
        listWcsPcedit(this.queryParams).then(
        response => {
        this.WcsPceditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // ID字典翻译
    trkIdFormat(row, column) {
      return this.selectDictLabel(this.trkIdOptions, row.trkId);
    },
    // 条码编号字典翻译
    contNoFormat(row, column) {
      return this.selectDictLabel(this.contNoOptions, row.contNo);
    },
    // 计划目的地字典翻译
    toPosFormat(row, column) {
      return this.selectDictLabel(this.toPosOptions, row.toPos);
    },
    // 实际目的地字典翻译
    curPosFormat(row, column) {
      return this.selectDictLabel(this.curPosOptions, row.curPos);
    },
    // 标识字典翻译
    dealFlagFormat(row, column) {
      return this.selectDictLabel(this.dealFlagOptions, row.dealFlag);
    },
    // 录入时间字典翻译
    createDateFormat(row, column) {
      return this.selectDictLabel(this.createDateOptions, row.createDate);
    },
    // 更新时间字典翻译
    updateDateFormat(row, column) {
      return this.selectDictLabel(this.updateDateOptions, row.updateDate);
    },
    // 手持强编号字典翻译
    spearIdFormat(row, column) {
      return this.selectDictLabel(this.spearIdOptions, row.spearId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams.contNo=null;
      //this.queryParams.spearId=null;
      this.resetForm("queryParams");
    },

    /** 搜索按钮操作 */
    handleQuery(e) {
     // console.log(this.queryParams)
          if(e.keyCode==13){
              // if(this.queryParams.spearId ==null || this.strlen(this.queryParams.spearId)==0){
              //     this.$message({type:"warning",message:"BCR编号不能为空"});
              //     return
              //   }
              // if(this.strlen(this.queryParams.spearId)<2){
              //     return
              // }
              if(this.queryParams.contNo ==null || this.strlen(this.queryParams.contNo)==0){
                  this.$message({type:"warning",message:"条码编号不能为空"});
                  return
              }
              this.queryParams.contNo=this.queryParams.contNo.toUpperCase();
            //  console.log(this.queryParams.contNo)
              this.Insert(this.queryParams);
              return
          }
    },
    
    Insert(queryParams){
          addWcsPcedit(this.queryParams).then(response => {
           //console.log(response)
            if (response.code === 200) {
              if(response.data==101){
                this.$message({type:"warning",message:response.msg});
              }else{
                this.msgSuccess("录入成功");
              }
              this.reset();
            }
          });
      },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.trkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据录入";
    },

    getVisualData(){
        // if(this.queryParams.spearId == null || this.strlen(this.queryParams.spearId)!=2)
        //     return
        this.getList();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const trkId = row.trkId || this.ids
      getWcsPcedit(trkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据录入";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.trkId != null) {
            updateWcsPcedit(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addWcsPcedit(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const trkIds = row.trkId || this.ids;
      this.$confirm('是否确认删除数据录入ID为"' + trkIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWcsPcedit(trkIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据录入数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWcsPcedit(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },

        /** 判断字符串长度方法 */
    strlen(str){
      if(str==null){
        return 0;
      }
      var len = 0;
      for (var i=0; i<str.length; i++) { 
        var c = str.charCodeAt(i); 
      //单字节加1 
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { 
          len++; 
        } 
        else { 
        len+=2; 
        } 
      } 
      return len;
    }


  }
  
};
</script>
